package org.simplecrm.domain.customer;

import org.hibernate.validator.constraints.NotBlank;
import org.simplecrm.domain.event.Event;
import org.simplecrm.domain.region.Region;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by oler on 09.09.14.
 */
@Entity
@Table(name = "Customer")
@NamedQueries({
        @NamedQuery(name = "Customer.findAll", query = "SELECT c FROM Customer c"),
        @NamedQuery(name = "Customer.count", query = "SELECT COUNT(c) FROM Customer c")
})
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    @Column(name = "name")
    @NotNull
    @NotBlank
    @Size(min = 2, max = 100)
    @Pattern(regexp = "[^ ][ a-zA-Z0-9\"]+")
    private String name;

    @Column(name = "total_income")
    private BigDecimal totalIncome;

    @ManyToOne(cascade = {CascadeType.MERGE}, fetch = FetchType.EAGER)
    @JoinColumn(name = "regionId", referencedColumnName = "id", nullable = false)
    @NotNull
    private Region region;

    @OneToMany(mappedBy = "customer",
            cascade = {CascadeType.PERSIST, CascadeType.REMOVE}, fetch = FetchType.EAGER)
    @OrderBy("creationDate DESC")
    private List<Event> events;

    public Customer() {
    }

    public Customer(Integer id) {
        this.id = id;
    }

    public Customer(String name, BigDecimal totalIncome, Region region, List<Event> events) {
        this.name = name;
        this.totalIncome = totalIncome;
        this.region = region;
        this.events = events;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigDecimal getTotalIncome() {
        return totalIncome;
    }

    public void setTotalIncome(BigDecimal totalIncome) {
        this.totalIncome = totalIncome;
    }

    public Region getRegion() {
        return region;
    }

    public void setRegion(Region regionByRegionId) {
        this.region = regionByRegionId;
    }

    public List<Event> getEvents() {
        return events;
    }

    public void setEvents(List<Event> eventsById) {
        this.events = eventsById;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Customer customer = (Customer) o;

        if (id != null ? !id.equals(customer.id) : customer.id != null) return false;
        if (name != null ? !name.equals(customer.name) : customer.name != null) return false;
        if (totalIncome != null ? !totalIncome.equals(customer.totalIncome) : customer.totalIncome != null)
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (totalIncome != null ? totalIncome.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", totalIncome=" + totalIncome +
                ", region=" + region +
                ", events=" + events +
                '}';
    }
}
